{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Lambda表达式",
   "id": "f068142890f1caab"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-10-27T08:45:25.532921Z",
     "start_time": "2024-10-27T08:45:25.529800Z"
    }
   },
   "source": [
    "l = [(3, -1), (2, 2), (0, 3), (4, 5), (3, 1), (5, 6)]\n",
    "def func(p):\n",
    "    return p[1]\n",
    "\n",
    "print(l)\n",
    "l.sort(key=func)\n",
    "print(l)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(3, -1), (2, 2), (0, 3), (4, 5), (3, 1), (5, 6)]\n",
      "[(3, -1), (3, 1), (2, 2), (0, 3), (4, 5), (5, 6)]\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-27T08:54:14.591535Z",
     "start_time": "2024-10-27T08:54:14.588542Z"
    }
   },
   "cell_type": "code",
   "source": [
    "goods_info =[\n",
    "    (\"1\", 12.5, 25),\n",
    "    (\"2\", 20, 25),\n",
    "    (\"3\", 10, 50),\n",
    "    (\"4\", 90, 100)\n",
    "]\n",
    "for i in goods_info:\n",
    "    print(f\"\\\"{i[0]}\\\": {i[1]/ i[2]}\")\n",
    "    \n",
    "goods_info.sort(key=lambda x: x[1] / x[2])\n",
    "print(goods_info)"
   ],
   "id": "42f1d15a8f36d9e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"1\": 0.5\n",
      "\"2\": 0.8\n",
      "\"3\": 0.2\n",
      "\"4\": 0.9\n",
      "[('3', 10, 50), ('1', 12.5, 25), ('2', 20, 25), ('4', 90, 100)]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-27T09:13:50.558378Z",
     "start_time": "2024-10-27T09:13:50.554929Z"
    }
   },
   "cell_type": "code",
   "source": [
    "res = lambda x: 3.14 * x * x\n",
    "print(f\"{res(3):.2f}\")\n",
    "(lambda x: 3.14 * x * x)(3)"
   ],
   "id": "a1263569a58150bb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28.26\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "28.259999999999998"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
